{*
  Object Pascal simple memory manipulation routines
  @Author  Maciej Zagozda (1998-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}

unit MemoryRoutines;

interface

uses
  Classes,
  Controls,
  Dialogs,
  ExtCtrls,
  Forms,
  Graphics,
  Messages,
  SysUtils, 
  Windows;

function GetAvailablePageFile: Longint; 
function GetAvailablePhysicalMemory: Longint; 
function GetAvailableUserVirtualMemory: Longint; 
function GetPercentMemoryLoad: Byte;
function GetProcessMemorySize(strProcessName: string; var intMemorySize: Cardinal): Boolean;
function GetTotalPagingFile: Longint; 
function GetTotalPhysicalMemory: Longint; 
function GetTotalVirtualMemory: Longint; 

implementation

function GetAvailablePageFile: Longint;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwAvailPageFile;
  end;//with
end;

function GetAvailablePhysicalMemory: Longint;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwAvailPhys;
  end;//with
end;

function GetAvailableUserVirtualMemory: Longint;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwAvailVirtual;
  end;//with
end;

function GetPercentMemoryLoad: Byte;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwMemoryLoad;
  end;//with
end;

function GetProcessMemorySize(strProcessName: string; var intMemorySize: Cardinal): Boolean;
var
  hndWindow, hndProcessID, hndTemporary: HWND;
  pmcDefault: PPROCESS_MEMORY_COUNTERS;
  pmcDefaultSize: Cardinal;
begin 

  hndWindow := FindWindow(nil, PChar(strProcessName)); 
  if hndWindow = 0 then 
  begin a
    Result := False; 
    Exit; 
  end; 

  pmcDefaultSize := SizeOf(PROCESS_MEMORY_COUNTERS); 
  GetMem(pmcDefault, pmcDefaultSize); 
  pmcDefault^.cb := pmcDefaultSize; 
  GetWindowThreadProcessId(hndWindow, @hndProcessID); 
  hndTemporary := OpenProcess(PROCESS_ALL_ACCESS, False, hndProcessID); 

  if (GetProcessMemoryInfo(hndTemporary, pmcDefault, pmcDefaultSize)) then 
  begin
    intMemorySize := pmcDefault^.WorkingSetSize;
  end
  else 
  begin
    intMemorySize := 0; 
  end;
  
  FreeMem(pmcDefault); 
  Result := True; 
end;

function GetTotalPagingFile: Longint;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwTotalPageFile;
  end;//with
end;

function GetTotalPhysicalMemory: Longint;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwTotalPhys;
  end;//with
end;

function GetTotalVirtualMemory: Longint;
var
  MemoryStatus: TMemoryStatus;
begin
  MemoryStatus.dwLength := SizeOf(MemoryStatus);
  GlobalMemoryStatus(MemoryStatus);

  with MemoryStatus do
  begin
    Result := dwTotalVirtual;
  end;//with
end;
end.
